package produceURL;

import java.io.IOException;
import java.sql.*;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class getStr {

	/**
	 * 
	 */
	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		Connection connGetSite = null;
		Statement stmtGetSite = null;
		ResultSet rsGetSite = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			SAXBuilder builder = new SAXBuilder(false);
			List sourceSiteParameterList = null;
			Document doc = builder.build("produceURL.xml");
			Element parameter = doc.getRootElement();
			sourceSiteParameterList = parameter.getChildren("URLList");
			for (Iterator iter = sourceSiteParameterList.iterator(); iter.hasNext();) {
				Element urlListParameter = (Element) iter.next();
				String dbHost = urlListParameter.getChildText("dbHost");
				String dbName = urlListParameter.getChildText("dbName");
				conn = DriverManager.getConnection("jdbc:mysql://" + dbHost + "/" + dbName + "?user=root");
				connGetSite = conn;
				stmt = conn.createStatement();
				stmtGetSite = connGetSite.createStatement();
				List urlList = urlListParameter.getChildren("url");
				for (Iterator iterurlList = urlList.iterator(); iterurlList.hasNext();) {
					Element urlParameter = (Element) iterurlList.next();
					String urlno = urlParameter.getTextTrim();
					rsGetSite = stmtGetSite.executeQuery("select DISTINCT url from tagurl where urlnotail = '" + urlno + "'");
					while (rsGetSite.next()) {
						stmt.execute("insert into url (url,urlNo) values ('" + rsGetSite.getString("url") + "','" + urlno + "')");
					}
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
				if (rsGetSite != null) {
					rsGetSite.close();
					rsGetSite = null;
				}
				if (stmtGetSite != null) {
					stmtGetSite.close();
					stmtGetSite = null;
				}
				if (connGetSite != null) {
					connGetSite.close();
					connGetSite = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

}
